/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Pacotes;

import Estruturas.Manipuladores;
import Opcoes.*;
import java.util.Random;

public class Header {
    
    private String id;      //  0-15
    private byte desc;      // 16
    private byte TTL;       // 17
    private byte hops;      // 18
    private byte[] PLength; // 19-22

    
    public Header(byte desc, int PLength) {
        
        this.desc = desc;
        this.PLength = new byte[4];
        this.PLength = Manipuladores.intToByteArray(PLength);

        byte[] ip = Manipuladores.IPtoByteArray(Manipuladores.getIP());
        
        Random rand = new Random();
        try
        {id = String.format("%c%c%c%c%s",(char)ip[0] , (char) ip[1] , (char) ip[2] , (char) ip[3], String.valueOf(rand.nextDouble()).substring(3,15));}
        catch(Exception e)
        {id = String.format("%c%c%c%c%s",(char)ip[0] , (char) ip[1] , (char) ip[2] , (char) ip[3], String.valueOf(rand.nextDouble()).substring(3,15));}
        
        TTL = (byte) Conexoes.PACKET_TTL;
        hops = (byte) Conexoes.PACKET_HOPS;
        
    }

    public String makeHeader() {
        //System.out.println("HEADER_PACK:" + id + desc + TTL + hops + (char)PLength[0]+(char)PLength[1]+(char)PLength[2]+(char)PLength[3]);
        //System.out.println("header_size:" + (int)PLength[0]+(int)PLength[1]+(int)PLength[2]+(int)PLength[3] + " DETECTED size:" + Manipuladores.byteArrayToInt(PLength, 0));
        return String.format("%s%c%c%c%c%c%c%c",id , desc , TTL , hops , (char)PLength[0] , (char)PLength[1] , (char)PLength[2] , (char)PLength[3]);
    }

    public int getPLength() {
        return Manipuladores.byteArrayToInt(PLength, 0);
    }

    public void setPLength(int PLength) {
        this.PLength = Manipuladores.intToByteArray(PLength);
    }

    public byte getTTL() {
        return TTL;
    }

    public void setTTL(byte TTL) {
        this.TTL = TTL;
    }

    public byte getDesc() {
        return desc;
    }

    public void setDesc(byte desc) {
        this.desc = desc;
    }

    public byte getHops() {
        return hops;
    }

    public void setHops(byte hops) {
        this.hops = hops;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    
    
}
